package com.coocaa.x.app.hall.parser.layoutparser;


import android.content.Context;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;

import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.coocaa.x.app.hall.R;

import java.util.List;

public class HorizontalScrollCustomLayout extends HorizontalScrollView {

    private OnScrollEndListener mOnScrollEndListener;
    private int smoothX = 0;
    private Context mContext;
    private BaseCustomLayout customLayout;

    public interface OnScrollEndListener {
        public void setScrollEnd(int x);
    }

    public HorizontalScrollCustomLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
        customLayout = new BaseCustomLayout(mContext);
        customLayout.setAnimation(R.anim.grid_anim);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
                Gravity.LEFT | Gravity.CENTER);

        customLayout.setLayoutParams(params);

        this.addView(customLayout);
    }

    @Override
    public void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);

        if (x == smoothX || x == 0 || (this.getWidth() + x >= this.computeHorizontalScrollRange())) {
            if (mOnScrollEndListener != null) {
                mOnScrollEndListener.setScrollEnd(x);
            }
        }
        this.getMeasuredWidth();
    }

    public int getComputeHorizontalScrollRange() {
        return this.computeHorizontalScrollRange();
    }

    public void smoothScroll(int x, int y) {
        super.smoothScrollTo(x, y);

        if (x > getScrollX() && this.getWidth() + this.getScrollX() >= this.computeHorizontalScrollRange()
                && mOnScrollEndListener != null) {
            mOnScrollEndListener.setScrollEnd(x);
        }
        smoothX = x;
    }

    @Override
    protected void onOverScrolled(int arg0, int arg1, boolean arg2, boolean arg3) {
        // TODO Auto-generated method stub
        super.onOverScrolled(arg0, arg1, arg2, arg3);

        //LogUtils.i("xfk0517", "onOverScrolled x: "+arg0+", compelet: "+arg2);
    }

    public void setOnScrollEndListener(OnScrollEndListener listener) {
        this.mOnScrollEndListener = listener;
    }

    public void setCustomLayoutParams(BaseCustomLayout.CustomLayoutParams params) {
        this.customLayout.setCustomLayoutParams(params);
    }

    public void setJson(String json) {
        this.customLayout.setJson(json);
    }

    public void setJson(List<String> jsonList) {
        this.customLayout.setJson(jsonList);
    }

    public void setmOnItemKeyEventListener(
            OnKeyListener mOnItemKeyEventListener) {
        this.customLayout.setOnItemKeyEventListener(mOnItemKeyEventListener);
    }

    public void setmOnItemFocusChangeListener(
            OnFocusChangeListener listener) {
        this.customLayout.setOnItemFocusChangeListener(listener);
    }

    public void setOnItemClickListener(OnClickListener listener) {
        this.customLayout.setOnItemClickListener(listener);
    }

    public void setmFocusChangedEvent(SlideFocusView.FocusChangedEvent focusChangedEvent) {
        this.customLayout.setFocusChangedEvent(focusChangedEvent);
    }

    public void setIItemViewLoadoverListener(
            BaseCustomLayout.IItemViewLoadOverListener listener) {
        this.customLayout.setIItemViewLoadOverListener(listener);
    }

    public void onDestroy() {
        mOnScrollEndListener = null;
        this.removeAllViews();
        if (customLayout != null) {
            customLayout.onDestroy();
        }
        customLayout = null;

        this.destroyDrawingCache();
    }
}
